/*
**
**   This is the user interface screen IO class for the
**   Student Registration System.
**
**
**
*/

import java.util.*;

public class ScreenIO  {
	//UI EVENT IDs
	private static final  int cLOGIN = 10001;
	private static final  int cNEWSTUDENT = 10002;
	private static final  int cEXIT = 10003;
	private static final  int cREGISTER = 10004;
	private static final  int cMAIN = 10005;
	private static final  int cUNREGISTER  = 10006;
	private static final  int cVIEWPROFILE = 10007;
	private static final  int cEDITPROFILE  = 10008 ;

	private String usernameLoggedIn;
	private String passwordLoggedIn;
	private String studentIdLoggedIn;
	private StudentRegistrationSystem regSystem;

	public ScreenIO() {
		usernameLoggedIn = new String("");
		passwordLoggedIn = new String("");
		studentIdLoggedIn = new String("");
		regSystem = new StudentRegistrationSystem();
	}
	public void beginUILoop(){
		int uievents = displayLandingScreen1();
		if (uievents == cEXIT) {
			regSystem.Close();
			return;
		}
		while(true) {//infinite UI loop :-)
		/*Inside this loop, we call a screen to display
		  then it returns the uiWvent of the option that was selected
		  we take that uiEvent and display the coorisponding
		  screen, rinse and repeat*/

			switch(uievents) {
				case(cLOGIN):
					uievents = cMAIN;
					if(displayLoginScreen2() == false) {
						uievents = cLOGIN; //cEXIT; //login failed
					}
					//System.out.printf("Login returned, uiEvent is set to %d\n", uievents);
					break;//these break the switch not the while
				case cNEWSTUDENT:
					uievents = displayNewProfileScreen6();
					break;
				case cREGISTER:
					uievents = cMAIN;
					Course crs = displayRegisterNewCourse();
					if(crs == null) {
						//already registered
					}
					else if(regSystem.registerCourse(studentIdLoggedIn, crs.getCourseID()) == false) {
						System.out.println("You are unable to register for this course at this time");
					}
					else
						System.out.println("User: " + studentIdLoggedIn + " Registered to course " + crs.getCourseID());
					break;
				case cMAIN:
					uievents = displayMainRegistrationScreen3();
					break;
				case cUNREGISTER:
					uievents = cMAIN;
					String unRegisterCourse = displayUnRegisterCourse();
					if(unRegisterCourse.isEmpty() == false) {
						if(regSystem.unRegsiterCourse(studentIdLoggedIn, unRegisterCourse) == false) {
							System.out.println("You are unable to unregister for this course at this time");
						}
					}
					else {
						System.out.println("Course not found.");
					}
					break;
				case cVIEWPROFILE:
					uievents = displayViewProfileScreen4();
					break;
				case cEDITPROFILE:
					uievents = displayEditProfileScreen5();
					break;
				default:
					uievents = cMAIN;
					break;
			}

			//I'm adding a seperate if statement so that I can break out of the while
			//a break in the switch, will just break out of the switch...
			if (uievents == cEXIT) {
				break; //break out of the while loop
			}

		}
	}

	private String displayUnRegisterCourse() {
		String sValue;
   		int iValue;
   		String retCourse = new String("");
		List<String> regCourseList = regSystem.getRegisteredCourses(studentIdLoggedIn);

   		System.out.println("====================================================================");
		System.out.println("|   STUDENT REGISTRATION SYSTEM                                    |");
		System.out.println("====================================================================");
		displayRegCourses(regCourseList, regSystem.getAvailableCourses());
 		System.out.println("|Withdraw from a Course                                               |");
       	sValue = inputKey.inString("Enter Course ID#: ");
		for (String strCourse : regCourseList) {
//			System.out.printf("StudentCourse: %s/n",strCourse.toString());
			if (strCourse.equals(sValue) ) {
				return sValue;
			}
		}
		System.out.println("");
		System.out.println("");
		sValue = "";
		return sValue;
	}

	private Course displayRegisterNewCourse() {
		String sValue;
   		int iValue;
   		Course retCourse = new Course("","","","",0,0,"","","","");
   		List<Course> availCourseList = regSystem.getAvailableCourses();
		List<String> regCourseList = regSystem.getRegisteredCourses(studentIdLoggedIn);

		System.out.println("====================================================================");
		System.out.println("|   STUDENT REGISTRATION SYSTEM                                    |");
		System.out.println("====================================================================");
		displayAvailCourses(availCourseList);
		System.out.println("| Register New Course                                               |");
		sValue = inputKey.inString("Enter Course ID#: ");
		System.out.printf("selected course: (%s)\n", sValue);

		for (Course course : availCourseList) {
			if (course.getCourseID().trim().equals(sValue.trim()) == true) {
				retCourse = course;
				break;
			}
		}
		for (String strCourse : regCourseList) {
			if (strCourse.trim().equals(sValue.trim()) == true) {
				System.out.println("You have already registered for this course\n\n");
				retCourse = null;
				break;
			}
		}
		return retCourse;

	}
	private void displayRegCourses(List<String> regCourseList, List<Course> availCourseList) {
		List<String> regCourses = new ArrayList<String>();

		System.out.printf( "%30s\t%10s\t%10s\t%40s\t%10s\t%10s\n", "Course Name", "Course ID#", "Start Date", "Summary", "Total Seats", "Total Enrolled");
		for (String regCourse : regCourseList) {
			    //System.out.printf("Course reg: %s\n", regCourse);
				for (Course course : availCourseList) {
						//System.out.printf("CourseAvil reg: %s\n", course.getCourseID());
						if(course.getCourseID().equals(regCourse)){
							System.out.printf( "%30s\t%10s\t%10s\t%40s\t%10d\t%10d\n", course.getCourseName(), course.getCourseID(), course.getBeginDate(), course.getCaryCourseSummary(), course.getEnrollmentLimit(), course.getNCurrentEnrol());
						}
				}
		}
		System.out.println("====================================================================");
	}
	private void displayAvailCourses(List<Course> availCourseList) {

		System.out.printf( "%30s\t%10s\t%10s\t%40s\t%10s\t%10s\n", "Course Name", "Course ID#", "Start Date", "Summary", "Total Seats", "Total Enrolled");
		for (Course course : availCourseList) {
			System.out.printf( "%30s\t%10s\t%10s\t%40s\t%10d\t%10d\n", course.getCourseName(), course.getCourseID(), course.getBeginDate(), course.getCaryCourseSummary(), course.getEnrollmentLimit(), course.getNCurrentEnrol());
		}
		System.out.println("====================================================================");
	}

	public int displayLandingScreen1() {

   		int iValue;
   		int retVal = cMAIN;

		System.out.println("====================================================================");
		System.out.println("|   STUDENT REGISTRATION SYSTEM                                    |");
		System.out.println("====================================================================");
		System.out.println("| Options:                                                         |");
		System.out.println("|        1. Login Existing Students                                |");
		System.out.println("|        2. New Student Sign up                                    |");
		System.out.println("|        3. Exit                                                   |");
		System.out.println("====================================================================");
		System.out.println("List of Available Courses");

		displayAvailCourses(regSystem.getAvailableCourses());
    	iValue = inputKey.inInt(" Select option: ");

		switch (iValue) {
		case 1:
		  System.out.println("Option 1 selected");
		  retVal = cLOGIN;
		  break;
		case 2:
		  System.out.println("Option 2 selected");
		  retVal = cNEWSTUDENT;
		  break;
		case 3:
		  System.out.println("Exit selected");
		  retVal = cEXIT;
		  break;
		default:
		  System.out.println("Invalid selection");
		  break;
    	}
    	return retVal;
	}

	private boolean displayLoginScreen2() {
		String username = new String("");
		String password = new String("");

		System.out.println("====================================================================");
		System.out.println("|   STUDENT LOGIN                                                 |");
		System.out.println("====================================================================");
		System.out.printf("| Login:");
		username = inputKey.inString("Username:");
		password = inputKey.inString(" password: ");


		if(regSystem.validateLoginCredentials(username, password)) {
			usernameLoggedIn = username;
			passwordLoggedIn = password;
			studentIdLoggedIn = regSystem.getStudentID(username);
			//System.out.println("PASSWORD VALID");
			return true;
		}

		return false;


	}
	private int displayMainRegistrationScreen3() {
		int retVal = cMAIN;
		List<String> regCourseList = regSystem.getRegisteredCourses(studentIdLoggedIn);
		System.out.println("====================================================================");
		System.out.println("|   STUDENT REGISTRATION SYSTEM - Welcome Screen                   |");
		System.out.println("====================================================================");
		System.out.printf("|Welcome: %s                   									   |\n", usernameLoggedIn);

		displayRegCourses(regCourseList,regSystem.getAvailableCourses());
		System.out.println("");
		System.out.println("");
		System.out.println("====================================================================");
		System.out.println("| Options:                                                          |");
		System.out.println("|        1. Register for a Course                                   |");
		System.out.println("|        2. UnRegister a Course                                     |");
		System.out.println("|        3. View profile                                            |");
		System.out.println("|        4. Edit profile                                            |");
		System.out.println("|        5. Exit                                                    |");
		System.out.println("====================================================================");
		System.out.println("====================================================================");

		switch (inputKey.inInt(" Please enter your option: ")) {
		case 1:
		  System.out.println("Option 1 selected - Register for a Course");
		  retVal = cREGISTER;
		  break;
		case 2:
		  System.out.println("Option 2 selected - UnRegister a Course");
  		  retVal = cUNREGISTER;
		  break;
		case 3:
		  System.out.println("Option 3 selected - View profile");
		  retVal = cVIEWPROFILE;
		  break;
		case 4:
		  System.out.println("Option 4 selected - Edit profile");
		  retVal = cEDITPROFILE;
		  break;
		case 5:
		  System.out.println("Exit selected");
		  retVal = cEXIT;
		  break;
		default:
		  System.out.println("Invalid selection");
		  break;
    	}
			System.out.println("");
			System.out.println("");
		return retVal;
	}

	private void displayProfileInfo(Student student) {
		System.out.printf("Loggedin username %s \n", usernameLoggedIn);
		System.out.printf("|   Welcome %s                                                       \n", student.fullName());
		System.out.printf("|Registration #: %s                                                  \n", student.userId());
		System.out.printf("|First Name : %s                                                     \n", student.firstName());
		System.out.printf("|Middle Name : %s                                                    \n", student.midName());
		System.out.printf("|Last Name : %s                                                      \n", student.lastName());
		System.out.printf("|Address 1: %s                                                       \n", student.address());
		System.out.printf("|Email Id: %s                                                        \n", student.email());
	}

	private int displayViewProfileScreen4() {
		int retVal = cMAIN;
		Student student = regSystem.getStudent(usernameLoggedIn);
		if (student == null) {
			System.out.println("Student does not exist!");
			retVal = cEXIT;
			return retVal;
		}
		System.out.println("====================================================================");
		System.out.println("|   STUDENT REGISTRATION SYSTEM - View Profile                     |");
		System.out.println("====================================================================");
		displayProfileInfo(student);
		System.out.println("====================================================================");
		System.out.println("| Options:                                                          |");
		System.out.println("|        1. To Main Screen                                          |");
		System.out.println("|        2. Exit                                                    |");
		System.out.println("====================================================================");

		switch (inputKey.inInt(" Please enter your option: ")) {
		case 1:
		  System.out.println("Option 1 selected - To Main Screen");
		  retVal = cMAIN;
		  break;
		case 2:
		  System.out.println("Exit selected");
		  retVal = cEXIT;
		  break;
		default:
		  System.out.println("Invalid selection");
		  break;
    	}
		return retVal;

	}
	private int displayEditProfileScreen5() {
		String sValue = new String ("");
    String strOldUsrName = usernameLoggedIn;//old username
	  String strOldPassword = passwordLoggedIn;//old password
		Student student = regSystem.getStudent(usernameLoggedIn);
		int retVal = cMAIN;
		System.out.println("====================================================================");
		System.out.println("|   STUDENT REGISTRATION SYSTEM - Edit Profile                     |");
		System.out.println("====================================================================");
		displayProfileInfo(student);
		System.out.println("====================================================================");
		System.out.println("|Update Profile Information");
		student = promptUserToEnterAccountInfo();
		System.out.println("====================================================================");
		System.out.println("| Options:                                                          |");
		System.out.println("|        1. Save                                                    |");
		System.out.println("|        2. Cancel                                                  |");
		System.out.println("|        3. Exit                                                    |");
		System.out.println("====================================================================");

		switch (inputKey.inInt(" Please enter your option: ")) {
		case 1:
		  System.out.println("Option 1 selected - Save");
	  	  String strNewUserName = usernameLoggedIn;//new username
		  String strNewPassword = passwordLoggedIn;//newPassword
		  regSystem.updateStudent(student);//update profile
          regSystem.changeLoginCredentials(strOldUsrName,strOldPassword,strNewUserName,strNewPassword);//update log in information
		  retVal = cMAIN;
		  break;
		case 2:
		  System.out.println("Option 2 selected - Cancel");
		  //do not save data
		  retVal = cMAIN;
		  break;
		case 3:
		  System.out.println("Exit selected");
		  retVal = cEXIT;
		  break;
		default:
		  System.out.println("Invalid selection");
		  break;
    	}
    	return retVal;
	}

	private Student promptUserToEnterAccountInfo() {
				String sValue = new String("");
				int inValue = 0;
				Student student = new Student("","","","","","","","","","",0,'m',0,"","","","",0);
				usernameLoggedIn = inputKey.inString(" username ");
				student.setUserId(usernameLoggedIn);
				passwordLoggedIn = inputKey.inString(" password ");
				sValue = inputKey.inString(" First Name ");
				student.setFirstName(sValue);
				sValue = inputKey.inString(" Middle Name ");
				student.setMidName(sValue);
				sValue = inputKey.inString(" Last Name ");
				student.setLastName(sValue);
				sValue = inputKey.inString(" Gender [M/F] ");
				student.setGender(sValue.charAt(0));
				sValue = inputKey.inString(" Address1 ");
				student.setAddress1(sValue);
				sValue = inputKey.inString(" Address2 ");
				student.setAddress2(sValue);
				sValue = inputKey.inString(" City ");
				student.setCity(sValue);
				sValue = inputKey.inString(" State ");
				student.setState(sValue);
				sValue = inputKey.inString(" Zip ");
				student.setZip(sValue);
				sValue = inputKey.inString(" Email ");
				student.setEmail(sValue);
				inValue = inputKey.inInt(" Age ");
				student.setAge(inValue);
				inValue = inputKey.inInt(" SSN ");
				student.setSSN(inValue);
				sValue = inputKey.inString(" Study Year [Fr,So,Ju,Se] ");
				student.setYearsOfStudy(sValue);
				student.setEnrollmentStatus(new String("Full Time"));
				sValue = inputKey.inString(" Major [CS,Eng,Sc,Hu,Lit] ");
				student.setEnrolledMajor(sValue);
				String strLname = student.lastName();
				String strMname = student.midName();
				String strFname = student.firstName();
				String strMjr = student.getEnrolledMajor();
				String strId = strLname.charAt(0) + strMname.charAt(0) + strFname.charAt(0) + strMjr;
				student.setId(strId);
				student.setRegdCourses(0);
				return student;
	}

	private int displayNewProfileScreen6() {
		String sValue = new String ("");
		int retVal = cMAIN;
		Student student ;//= regSystem.getStudent(usernameLoggedin);
		System.out.println("====================================================================");
		System.out.println("|   STUDENT REGISTRATION SYSTEM - New Profile                     |");
		System.out.println("====================================================================");
		System.out.println("====================================================================");
		System.out.println("|Enter Profile Information");
		student = promptUserToEnterAccountInfo();
		System.out.println("====================================================================");
		System.out.println("| Options:                                                          |");
		System.out.println("|        1. Save                                                    |");
		System.out.println("|        2. Cancel                                                  |");
		System.out.println("|        3. Exit                                                    |");
		System.out.println("====================================================================");

		switch (inputKey.inInt(" Please enter your option: ")) {
		case 1:
		  System.out.println("Option 1 selected - Save");
		  regSystem.addNewStudent(student, passwordLoggedIn);
		  System.out.printf("Add new student: %s\n", usernameLoggedIn);
		  retVal = cMAIN;
		  break;
		case 2:
		  System.out.println("Option 2 selected - Cancel");
		  //do not save data
		  retVal = cMAIN;
		  break;
		case 3:
		  System.out.println("Exit selected");
		  retVal = cEXIT;
		  break;
		default:
		  System.out.println("Invalid selection");
		  break;
    	}
    	return retVal;

	}
}